using System;
using System.Collections.Generic;
using Rilisoft.MiniJson;
using UnityEngine;

public class MapDataDownloader : MonoBehaviour
{
	public static MapDataDownloader instance;

	private static string defaultJson = "{\n\t\"pvp1_island\": 1,\n\t\"pvp2_invincible\": 1,\n\t\"pvp3_military\": 1,\n\t\"pvp4_china\": 1,\n\t\"pvp5_beach\": 1,\n\t\"pvp6_port\": 1,\n\t\"pvp7_stadium\": 1,\n\t\"pvp8_hospital\": 1,\n\t\"pvp9_road66\": 1,\n\t\"pvp11_Zombie\": 1,\n\t\"pvp10_Roofs\": 1\n}\n";

	public Dictionary<string, bool> mapData = new Dictionary<string, bool>();

	private bool JsonFileLoaded;

	public static event Action MapsDataUpdated;

	private void Awake()
	{
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		instance = this;
		ParsJSON(JSONManager.Instance.GetJSON(URLs.MapData));
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void ParsJSON(string text)
	{
		if (!JsonFileLoaded)
		{
			if (text.Equals(string.Empty))
			{
				text = defaultJson;
			}
			LoadSettingFromJSON(text);
		}
	}

	private void LoadSettingFromJSON(string text)
	{
		Dictionary<string, object> dictionary = Json.Deserialize(text) as Dictionary<string, object>;
		if (dictionary == null)
		{
			BCWDebug.LogError("Cant Deserialize data!");
			return;
		}
		try
		{
			foreach (string key in dictionary.Keys)
			{
				object value = 0;
				if (dictionary.TryGetValue(key, out value))
				{
					mapData.Add(key, Convert.ToInt32(value) > 0);
				}
				else
				{
					mapData.Add(key, true);
				}
			}
			JsonFileLoaded = true;
			if (MapDataDownloader.MapsDataUpdated != null)
			{
				MapDataDownloader.MapsDataUpdated();
			}
		}
		catch (Exception ex)
		{
			BCWDebug.LogError("Error parse json " + ex);
			Debug.LogError(text);
			BCWDebug.LogError("Parse default Json " + defaultJson);
			LoadSettingFromJSON(defaultJson);
		}
	}
}
